def search
  if @cgi.params['mode'][0] == 'search' then
    result = []
    files = []
    word = @cgi.params['word'][0]
    
    @file_extension.each do |fext|
      files += Dir.glob("#{@datadir}//**/*.#{fext}")
    end
    
    files.each {|f|
      File::open(f){|fh|
        fh.each{|line|
          if line.include?(word) then
            result.push("#{f}")
            break
          end
        }
      }
    }
    
    @stories = result
    
    if @stories.size > 0
      sw = {}
      begin
        File::open("#{@plugindir}/search.log"){|f|
          f.each_line do |ln|
            m = ln.split(/\t/)
            sw[m[0]] = m[1].to_i
          end
        }
      rescue
      end
      if sw[word]
        sw[word] += 1
      else
        sw[word] = 1
      end
      
      sw = sw.to_a.sort_by {|a| a[1]}
      
      File::open("#{@plugindir}/search.log",'w'){|f|
        sw.each do |m|
          f.print "#{m[0]}\t#{m[1]}\n"
        end
      }
      
      @search_word = word
    end
    
  end
end

def search_form
  src = <<EOS
<p>
<form id="find" name="find" method="GET" action="#{@url}">
  <input type="hidden" name="mode" value="search" />
  <label for="search" accesskey="4">Search this site:</label><br />
  <input type="text" id="search" name="word" value="#{@search_word}" />
  <input type="submit" value="Search" />
</form>
</p>
EOS

  return src
end

def search_log
  src = "<ul>\n"
  begin
    sw = {}
    File::open("#{@plugindir}/search.log"){|f|
      f.each_line do |ln|
        m = ln.split(/\t/)
        sw[m[0]] = m[1].to_i
      end
    }
    
    sw.to_a.sort_by {|a| a[1]}.each do |m|
      src += %(<li><a href="#{@url}?mode=search&word=#{CGI.escape(m[0])}">)
      src += %(#{m[0]}</a> x #{m[1]}</li>\n)
    end

    src += "</ul>\n"
  rescue
  end
  
end
